<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
  >
    <template #footer>
      <a-button @click="closeModal">取消</a-button>
      <a-button type="primary" @click="handleSubmit(false)">保存</a-button>
      <a-button type="primary" @click="handleSubmit(true)"
        >保存并继续添加</a-button
      >
    </template>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicModal, useModalInner } from '@/components/Modal';
import { BasicForm, useForm } from '@/components/Form/index';
import { formSchema } from './data';
import { useMessage } from '@/hooks/web/useMessage';
import { edit, add } from '@/api/sys/sys/sysExportConfigManage';
const emit = defineEmits(['success']);
let isUpdate = ref(true);
const { createMessage } = useMessage();
const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 100,
  schemas: formSchema,
  showActionButtonGroup: false,
  baseColProps: { span: 24 },
});

const [registerModal, { setModalProps, closeModal }] = useModalInner(
  async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;

    if (unref(isUpdate)) {
      setFieldsValue({
        ...data.record,
      });
    }
  }
);

// const sourceColumn = ref([]);
// const transResult = ref([]);

// const handleTrans = () => {
//   try {
//     const inputColumn = eval(sourceColumn.value);
//     let resultColumn = [];
//     transResult.value = JSON.stringify(
//       transExcelConfig(inputColumn, resultColumn)
//     );
//     console.log('转换结果', resultColumn);
//   } catch (error) {
//     console.error('输入有误');
//   }
// };
const transExcelConfig = (oL, nL) => {
  oL.forEach((col, index) => {
    nL.push({
      secdFieldName: col.dataIndex,
      secdName: col.title,
      secdOrder: index + 1,
      secdWidth: col.width ? Math.round(col.width / 6) : 20,
      secdHeight: 10,
      ...getType(col.title),
    });
    if (col.children && col.children.length) {
      transExcelConfig(col.children, nL);
    }
  });
  return nL;
};

const getType = (title) => {
  if (title.indexOf('日期') > -1) {
    return {
      secdType: 1,
      secdTimeZone: 'GMT+8',
      secdFormat: 'yyyy-MM-dd',
    };
  }
  if (title.indexOf('时间') > -1) {
    return {
      secdType: 1,
      secdTimeZone: 'GMT+8',
      secdFormat: 'yyyy-MM-dd HH:mm:ss',
    };
  }
  if (title.indexOf('金额') > -1 || title.indexOf('面积') > -1) {
    return {
      secdType: 10,
      secdNumFormat: '#.##',
    };
  }
  return {
    secdType: 1,
  };
};

const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

async function handleSubmit(hasNext: boolean) {
  try {
    const values = await validate();
    setModalProps({ confirmLoading: true });
    const { details } = values;
    const inputColumn = eval(details);
    const resultColumn = []
    transExcelConfig(inputColumn, resultColumn);
    const data = { ...values, details: resultColumn };
    if (unref(isUpdate)) {
      await edit(data);
      createMessage.success('修改成功');
    } else {
      await add(data);
      createMessage.success('保存成功');
    }
    if (hasNext) {
      resetFields();
    } else {
      closeModal();
    }
    emit('success');
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
